<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_documents_Menu extends m_website_documents_MenuBase
{
	private static $instances;
	
	/**
	 * @return m_website_documents_Menu
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_website_documents_Menu
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		
		if(!isset(self::$instances[$lang][$id]))
		{
			$cache = f_core_Cache::getInstance();
			
			$key = $cache->getDocumentCacheKey($id, $lang);
			
			if($cache->hasCache($key))
			{
				return $cache->getCacheContent($key);
			}
					
			self::$instances[$lang][$id] = new self($id, $lang);
			
			$cache->putInCache($key, self::$instances[$lang][$id]);
			
			return self::$instances[$lang][$id];		
		}		
		return self::$instances[$lang][$id];
	}

    /**
     * Delete child pages and child menus before deleting current menu
     */
    public function preDelete()
    {
        if($this->getPageNb() > 0)
        {
            foreach($this->getPage() as $page)
            {
                $page->delete(false);
            }
        }

        if($this->getMenusNb() > 0)
        {
            foreach($this->getMenus() as $menu)
            {
                $menu->delete(false);
            }
        }
    }
	
	public function canSlide()
	{
		return true;
	}
	
	public function getLabel()
	{
		if($this->isATranslation() && !$this->isTranslated())
		{
			$voDoc = $this->getVoDocument();
			return $voDoc->getLang()." / ".$voDoc->getLabel();
		}
		return parent::getLabel();
	}
	
	public function getNewDocumentLabel()
	{
		return "Nouveau menu";
	}
	
	public function getIcon()
	{
        return 'menu_menu.png';
	}
	
	public function getEditUrl($lang = null)
	{
		if(!is_string($lang) && is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang", $this->getLang());
		}
		
		if(!$this->isTranslated())
		{
			return f_core_Routing::getUrl("menuTranslate", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
		}
		
		return f_core_Routing::getUrl("menuEdit", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}
	
	public function getDeleteUrl($lang = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}

		return f_core_Routing::getUrl("deleteMenu", array("docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}
	
	public function getUrl()
	{
		if(!is_null($this->getMenuPageId()) && $this->getMenuPageId() != '0')
		{
            return f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $this->getMenuPageId(), $this->getLang())->getCompleteUrl();
		}
		
		return '';
	}

	public function getIndexMenu()
	{
		$docProvider = f_document_Provider::getInstance();

		$document = $docProvider->loadDocuments("modules_website/menu")
								->whereIsEqual("menuPageId", $this->getUniqueId())
								->retrieveFirstDocument($this->getLang());

		if($document instanceof f_document_BaseDocument)
		{
			return $document;
		}

		return null;
	}

    public function postSave()
    {
         $tpl = f_template_Template::getInstance();
         $tpl->clear_all_cache();
    }

    public function getBoSearchIcon()
    {
        return m_medias_services_MediaService::getInstance()->getIconPath($this->getIcon());
    }
}